#!/bin/bash

#------------------------------------------------------------------------------#
# SVG2PDF
# http://code.google.com/p/video2android/
#
# Author:	Javier Pérez <www@javierperez.eu>
# Blog:		http://javierperez.eu | http://en.javierperez.eu
# Twitter:	http://twitter.com/javierperez
#
# License:	GPL v3
#
#------------------------------------------------------------------------------#

for arg; do
	# fichero de video fuente
	INPUTFILE=$arg

	# fichero de video destino
	OUTPUTDIR=`dirname $INPUTFILE`
	EXT=${INPUTFILE#*.}
	OUTPUTFILE=$OUTPUTDIR/`basename $INPUTFILE .$EXT`.mp4

	OUTPUTFILE=`zenity --file-selection --save --confirm-overwrite --title="Fichero MP4" --filename=$OUTPUTFILE`
	if [ $? -eq 0 ]; then
		# video: bitrate
		VIDEO_BR=`zenity --entry --text="video bitrate" --entry-text="512"`

		# audio: bitrate
		AUDIO_BR=`zenity --entry --text="audio bitrate" --entry-text="96"`

		# resumen
		zenity --question --text="Convertir $INPUTFILE a $OUTPUTFILE con los valores video-br=$VIDEO_BR y audio-br=$AUDIO_BR" --ok-label="convertir" --cancel-label="cancelar"
		if [ $? -eq 0 ]; then
			(
			# go!
			mencoder "$INPUTFILE" -o "$OUTPUTFILE" \
			  -vf dsize=480:352:2,scale=-8:-8,harddup \
			  -oac faac -faacopts mpeg=4:object=2:raw:br=${AUDIO_BR} \
			  -of lavf -lavfopts format=mp4 -ovc x264 -sws 9 \
			  -x264encopts nocabac:level_idc=30:bframes=0:bitrate=${VIDEO_BR}:threads=auto:turbo=1:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh
			) | zenity --progress --pulsate --auto-close
			if [ $? -eq 0 ]; then
				zenity --info --text="Fichero de video creado: $OUTPUTFILE"
			else
				zenity --warning --text="Se canceló la conversión de video"
			fi
		fi
	fi
done

